package com.lgrglw.search.dao;


import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SortField;
import org.springframework.stereotype.Repository;

import com.lgrglw.common.spring.PropertyOper;
import com.lgrglw.search.mapping.Demoentity;

@Repository
public class TestSearchDao extends IndexBaseDao<Demoentity,String> {
	
	@SuppressWarnings("deprecation")
	public void search()throws Exception{
		IndexReaderOper oper=new IndexReaderOper(PropertyOper.getInstance().getIndexDir()+"demoentity");
		Analyzer analyzer=new org.wltea.analyzer.lucene.IKAnalyzer();
		BooleanQuery sum = new BooleanQuery();
		QueryParser queryParser = new MultiFieldQueryParser(new String[]{"name","menuTitle"},analyzer);
		queryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
        Query highlightQuery=queryParser.parse("银行");
		sum.add(highlightQuery,BooleanClause.Occur.MUST);
		BooleanQuery path = new BooleanQuery();
		SearchTools.buildQueryForTerm(path, "path", "200", SearchContants.SHOULD);
		SearchTools.buildQueryForTerm(path, "path", "300", SearchContants.SHOULD);
		sum.add(path,BooleanClause.Occur.MUST);
		Hits hit=oper.search(sum, null);
		for(int i=0;i<hit.length();i++){
			Document doc=hit.doc(i);
			String method=doc.getField("name").stringValue();
			System.out.println("第"+(i+1)+"条记录:  "+doc.getBoost()+"   "+method);
		}
		oper.end();
	}
	
}
